package com.ng.stat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ng.stat.dao.BookClsDao;
import com.ng.stat.entity.BookSortEntity;
import com.ng.stat.service.BookClsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cb
 * @Description TODO
 * @date 2023/3/19
 */
@Service
public class BookClsServiceImp implements BookClsService {

    @Autowired
    BookClsDao BookClsdao;

    @Override
    public List<BookSortEntity> querybookcls(BookSortEntity bcls) {

        List<BookSortEntity> nb=new ArrayList<BookSortEntity>();
        QueryWrapper<BookSortEntity> ss=new QueryWrapper<BookSortEntity>();
       // ss.eq(bcls.getGrade()!=0,"grade", bcls.getGrade());
        ss.likeRight(StringUtils.isNotBlank(bcls.getCode()),"code", bcls.getCode());
        nb= BookClsdao.selectList(ss);
        nb=GetNode(nb,bcls);

        return nb;
    }
    @Override
    public List<BookSortEntity> querybookclsnochild(BookSortEntity bcls){
        List<BookSortEntity> nb=new ArrayList<BookSortEntity>();
        QueryWrapper<BookSortEntity> ss=new QueryWrapper<BookSortEntity>();
        ss.eq(bcls.getGrade()!=0,"grade", bcls.getGrade());
        ss.likeRight(StringUtils.isNotBlank(bcls.getCode()),"code", bcls.getCode());
        nb= BookClsdao.selectList(ss);
        return nb;
    }


    @Override
    public BookSortEntity querybookclsbycode(BookSortEntity bcls) {

        BookSortEntity nb=new BookSortEntity();
        QueryWrapper<BookSortEntity> ss=new QueryWrapper<BookSortEntity>();

        ss.eq(StringUtils.isNotBlank(bcls.getCode()),"code", bcls.getCode());
        nb= BookClsdao.selectOne(ss);
        return nb;
    }

    private List<BookSortEntity> GetNode(List<BookSortEntity> dt,BookSortEntity bcls)
    {
        List<BookSortEntity> ncList = new ArrayList<BookSortEntity>();


        //DataRow[] rowArray = !string.IsNullOrEmpty(classCode) ? dt.Select().Where(datarow => datarow["GRADE"].ToString().Equals("1") && datarow["Code"].ToString().Equals(classCode)).ToArray() : dt.Select().Where(datarow => datarow["GRADE"].ToString().Equals("1")).ToArray();
        List<BookSortEntity> dt2=new ArrayList<>();
        if(  bcls.getGrade()==0) {
             dt2 = dt.stream().filter(s -> {
                return s.getGrade() == 1;
            }).collect(Collectors.toList());
        }else {
            dt2=dt.stream().filter(s -> {
                return s.getGrade() == bcls.getGrade();
            }).collect(Collectors.toList());;
        }

        for( BookSortEntity nodeitem : dt2)
        {
            BookSortEntity childnode = new BookSortEntity();
            childnode.setName(nodeitem.getName());
            childnode.setCode(nodeitem.getCode());
            childnode.setGrade(nodeitem.getGrade());
            childnode.setSyscode(nodeitem.getSyscode());
            GetChildNode(dt, childnode);
            ncList.add(childnode);
        }
        return ncList;
    }

    private void GetChildNode(List<BookSortEntity> dt, BookSortEntity node)
    {
        int grade = node.getGrade() + 1;
        List<BookSortEntity> dt2=dt.stream().filter(s->{
            return s.getGrade()==grade;
        }).collect(Collectors.toList());
        List<BookSortEntity>bb=new ArrayList<>();
        for (BookSortEntity item : dt2)
        {
            if (node.getSyscode().equals(item.getSyscode().substring(0, item.getSyscode().length() - 4)))
            {
                BookSortEntity childnode = new BookSortEntity();
                childnode.setName(item.getName());
                childnode.setCode(item.getCode());
                childnode.setGrade(item.getGrade());
                childnode.setSyscode(item.getSyscode());


               // node.child.add(childnode);
                GetChildNode(dt, childnode);
                bb.add(childnode);
                node.setChild(bb);

            }
        }
    }

}
